let data = [
  { price: 4, name: 'A-1' },
  { price: 2, name: 'B-1' },
  { price: 3, name: 'C-1' },
  { price: 1, name: 'A2-1' },
];
function foo() {
  // your code
  // 保存结果
  this.res = null;
  // 接收foo的参数
  let args = Array.from(arguments[0]);
  // 保存this
  let context = this;
  let search = function (arr) {
    return function (property, type) {
      if (!Array.isArray(arr)) {
        throw new Error('不是数组');
      }
      if (type == 'desc') {
        arr.sort((a, b) => a[property] - b[property]);
      } else {
        arr.sort((a, b) => b[property] - a[property]);
      }
      this.res = arr;
      return context;
    };
  };
  let where = function (arr) {
    return function (property, regStr) {
      if (!Array.isArray(arr)) {
        throw new Error('不是数组');
      }
      let filterData = arr.filter((item) => regStr.test(item[property]));
      this.res = filterData;
      context.where = where(filterData);
      context.search = search(filterData);
      return context;
    };
  };
  this.where = where(args);
  this.search = search(args);
  return this;
}
foo(data).where('name', /^A/).search('price', 'desc').where('name', /^A/);

console.log(res);
//返回
// res = [{ 'price': 4, name: 'A2-1' }, { 'price': 1, name: 'A-1' }]
